package com.wjbgn.bsolver.server.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.wjbgn.bsolver.server.convert.CategoryDoConvert;
import com.wjbgn.bsolver.server.dto.CategoryDTO;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjbgn.bsolver.server.entity.CategoryDO;
import com.wjbgn.bsolver.server.service.ICategoryService;
import com.wjbgn.bsolver.server.mapper.CategoryMapper;

import java.util.List;

/**
 * Description:
 * Create Date: 2022-04-16T14:28:22.285
 * @author wjbgn
 * @version 1.0
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, CategoryDO> implements ICategoryService {

    public List<Tree<Long>> getCategoryTree(){
        List<CategoryDO> categoryDOS = this.list();
        List<CategoryDTO> categoryDTOS = CategoryDoConvert.listConvert(categoryDOS);

        List<Tree<Long>> build = TreeUtil.build(categoryDTOS,0L,(object,tree)->{
            tree.putExtra("id",object.getId());
            tree.putExtra("value",object.getId());
            tree.putExtra("parentId",object.getParentId());
            tree.putExtra("label",object.getName());
        });

        return build;
    }
}